import { plugin, Messagetype } from 'alemon'
import db from '../../db/sqlite/config.js'
import { QueryTypes } from 'sequelize'

export class K_gjcDel extends plugin {
  constructor() {
    super({
      dsc: '删除关键词',
      priority: 500,
      rule: [
        {
          reg: '^/删除(.*)$',
          fnc: '方法名'
        }
      ]
    })
  }
  async 方法名(e: Messagetype) {
    const eventType = e.eventId.split(':')[0]
    // 只允许私域消息
    if (eventType !== 'MESSAGE_CREATE') {
      return
    }

    // 1	全体成员
    // 2	管理员
    // 4	群主/创建者
    // 5	子频道管理员
    // 没有权限
    if (
      !e.msg.member.roles.includes('4') &&
      !e.msg.member.roles.includes('5') &&
      !e.msg.member.roles.includes('2')
    ) {
      await e.reply(`<@!${e.msg.author.id}> 无权限`)

      return true
    }

    const msg = e.msg.content
      .substring(e.msg.content.indexOf('/删除'), e.msg.content.length)

      .replace('/删除', '')
      .trim()

    if (msg === '') {
      await e.reply(`<@!${e.msg.author.id}> 内容不能为空!`)
      return true
    }

    await db.query(`DELETE FROM k_gjc WHERE key = ? and guild =?`, {
      replacements: [msg, e.msg.guild_id],
      type: QueryTypes.DELETE
    })
    return e.reply(`<@!${e.msg.author.id}> 删除成功!`)
  }
}
